package com.kgc.provider;

import com.alibaba.dubbo.config.annotation.Service;
import com.kgc.api.goods.HotWordApi;
import com.kgc.bean.HotWord;
import com.kgc.domain.HotWordExample;
import com.kgc.mapper.HotWordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.List;

/**
 * Created on 2021/6/22.
 * Author: lzy
 * Description:
 */
@Service
@Slf4j
public class HotWordProvider implements HotWordApi {

    @Autowired
    private HotWordMapper hotWordMapper;


    @Override
    public HotWord querySelectHotWord(String hotwords) {
        HotWord hotWord = new HotWord();
        log.info(" 传如的查询参数,{}",hotwords);
        com.kgc.domain.HotWord hotWord1 = hotWordMapper.selectByHotWord(hotwords);
        // log.info("查出的搜索量：{}",hotWord1.getHotWordCount());
        if(!ObjectUtils.isEmpty(hotWord1)){

            BeanUtils.copyProperties(hotWord1,hotWord);
        }
        log.info("数据为：{}",hotWord);
        // 如果空，返回的false ,如果不为空，返回true;
        return hotWord;
    }

    @Override
    public boolean queryAddNewHotWord(HotWord hotWord) {
        com.kgc.domain.HotWord hotWord1 = new com.kgc.domain.HotWord();
        BeanUtils.copyProperties(hotWord,hotWord1);
        log.info("web端传的数据为：{}",hotWord1);
        return hotWordMapper.insert(hotWord1) > 0;
    }

    @Override
    public boolean queryUpdateCordToHotWord(HotWord hotWord) {
        com.kgc.domain.HotWord hotWord1 = new com.kgc.domain.HotWord();

        BeanUtils.copyProperties(hotWord,hotWord1);
        return hotWordMapper.updateByPrimaryKeySelective(hotWord1) > 0;
    }

    @Override
    public List<HotWord> selectHotWord5() {
        List<HotWord> hotWords = new ArrayList<>();
        HotWordExample hotWordExample = new HotWordExample();
        hotWordExample.setDistinct(true);
        hotWordExample.setOrderByClause("hot_word_count desc");
        int num = 0;
        hotWordExample.setOffset((long) num);
        hotWordExample.setLimit(5);
        List<com.kgc.domain.HotWord> hotWordList = hotWordMapper.selectByExample(hotWordExample);
       // 遍历，转换
        log.info("查询的热词数据有多少条{}",hotWordList.size());
       for (int i = 0 ; i < hotWordList.size();i++){
            HotWord hotNewWord = new HotWord();
            BeanUtils.copyProperties(hotWordList.get(i),hotNewWord);
           hotWords.add(hotNewWord);
       }
        return hotWords;
    }



}
